﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using System.Data;
using Honestysoft.Financing.Model;
using Honestysoft.BaseObject;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    public partial class SelectDocRec : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                BindControl();
                Bind();
            }
        }
        /// <summary>
        /// 绑定页面控件。
        /// </summary>
        private void BindControl()
        {
            ControlHelper.BindDictionary(ddlContractCategory, "ContractCategory");
            ControlHelper.InsertAllItemAndSelect(ref ddlContractCategory);
        }
        /// <summary>
        /// 设置弹出窗体大小
        /// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
        protected override void SetDialogSize()
        {
            base.SetDialogSize();
            //重载宽度，高度
            //ModalDialogSize.EditHeight =250;
            //ModalDialogSize.EditWidth = 500;
            //ModalDialogSize.ViewHeight = 250;
            //ModalDialogSize.ViewWidth = 500;
        }
        /// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
               // btnAdd.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/Bu_DocReceive_Edit.aspx?FunctionID=" + FunctionID + "&ContractID=" + Request.QueryString["ID"] + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表');return false;");
            }
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void Bind()
        {
            Honestysoft.Financing.BusinessLogic.Audit bll = new BusinessLogic.Audit(Globals.CurrentConnStringName);
            string projectName = "";
            string contractCategory = ddlContractCategory.SelectedValue;
            if (contractCategory == "0") { contractCategory = null; }
            string code = txtCode.Text.Trim();
            if (code.Length == 0) { code = null; }
            string name = txtName.Text.Trim();
            if (name.Length == 0) { name = null; }
            //string[] arrId = null;
            //if (hidFilterContractIDString.Value.Length > 0)
            //{
            //    arrId = hidFilterContractIDString.Value.Split(',');
            //}
            if (txtProjectName.Enabled)
            {
                if (txtProjectName.Text.Trim().Length > 0)
                {
                    projectName = txtProjectName.Text.Trim();
                }
            }
            DataTable dtList = bll.GetCanSelectDocReceive(null, projectName, contractCategory, code, name);
            rptList.DataSource = dtList;
            rptList.DataBind();
        }
        /// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId)
        {
            string script = "";
            //if (PageSecurity.Edit)
            //{
            //    script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Audit/Bu_DocReceive_Edit.aspx", "修改资料接收表", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID);
            //}
            //if (PageSecurity.Delete)
            //{
            //    if (script.Length > 0) { script += "&nbsp;"; }
            //    script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
            //}
            if (PageSecurity.View)
            {
                if (script.Length > 0) { script += "&nbsp;"; }
                script += ScriptHelper.LinkButton.GetGridViewButton(Globals.Home + "Module/Audit/Bu_DocReceive_View.aspx", "查看资料接收表", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID);
            }
            return script;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string batchID = Request.QueryString["ID"];
            string[] arrRecID = GetSelectedRowKeys();
            //List<Bu_BatchRec> lsBatchRes = new List<Bu_BatchRec>();
            //Bu_BatchRec item = null;
            //foreach (string s in arrRecID)
            //{
            //    item = new Bu_BatchRec();
            //    item.BatchID = batchID;
            //    item.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            //    item.RecID = s;
            //    item.IsBack = false;
            //    item.IsReceive = false;
            //    item.IsSend = false;
            //    item.TableCode = Guid.NewGuid().ToString();
            //    item.TableID = Guid.NewGuid().ToString();
            //    lsBatchRes.Add(item);
            //}
            try
            {
                bool bRet = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).AddBatchRec(UserLoginer.LoginUser.ID, batchID, arrRecID);
                //QueryParameters query = new QueryParameters();
                //query.Add("BatchID", batchID, QueryParameters.OperatorType.Equal);
                //DataTable dt = new BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName).GetTable(query);
                //foreach (DataRow dr in dt.Rows)
                //{
                //    AuditUtils.ExportToExcel(dr["ID"].ToString());
                //}
                ScriptHelper.Window.Alert("成功导入（" + arrRecID.Length.ToString() + "）条回执！", "hideDiv();window.returnValue=true;window.close();");
            }
            catch (Exception ex)
            {
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("导入回执失败！"+ex.Message);
            }
        }
    }
}
